package com.dragon.array;

/**
 * @author dragon
 * @version 1.0
 * @description
 * @date created in 2018-12-21
 */
public class Sum {

    /**
     * 递归求和
     *
     * @param arr
     * @return
     */
    public static int sum(int[] arr) {
        return sum(arr, 0);
    }

    public static void print(int[] arr) {
        print(arr, 0);
    }

    private static void print(int[] arr, int l) {
        if (l == arr.length - 1) {
            System.out.println(arr[l]);
            return;
        }
        System.out.print(arr[l] + ",");
        print(arr, l + 1);
    }

    private static int sum(int[] arr, int l) {
        if (l == arr.length) {
            return 0;
        }
        return arr[l] + sum(arr, l + 1);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
        System.out.println(sum(arr));
        print(arr);
    }
}
